package com.kaisa.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.fastjson.JSONObject;
import com.kaisa.activity.R;
import com.kaisa.activity.bean.CountriesBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class CountryActivity extends AppCompatActivity {

    public static final String PATH_CONTRY = "http://appsever.caissa.com.cn/api/destination/searchAll?account=123&channel=CS0001&password=123&channelSecond=a0002&terminal=android&sourceFlag=865863026688320";

    @ViewInject(R.id.rg_content)
    private RadioGroup contentRG;
    @ViewInject(R.id.lv_contry_content)
    private ListView contentLV;

    private ArrayAdapter adapter;

    private List<CountriesBean.ContinentListEntity> list = new ArrayList<CountriesBean.ContinentListEntity>();
    private List<CountriesBean.ContinentListEntity.CountryListEntity> listcontry = new ArrayList<CountriesBean.ContinentListEntity.CountryListEntity>();
    private List<String> arrayList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_country);
        x.view().inject(this);

        initView();
        initData();
        addListener();
    }

    private void initView() {
        adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
        contentLV.setAdapter(adapter);

    }

    private void initData() {
        x.http().get(getParams(), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                CountriesBean bean = JSONObject.parseObject(s, CountriesBean.class);
                list.clear();
                list.addAll(bean.getContinentList());
                for (int i = 0; i < list.size(); i++) {
                    ((RadioButton) (contentRG.getChildAt(i))).setText(list.get(i).getRegion_Name());
                }
                ((RadioButton) (contentRG.getChildAt(0))).setChecked(true);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });





    }

    private void addListener() {
        contentRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int tag = 0;
                switch (i) {
                    case R.id.rb_1:
                        tag = 0;
                        break;
                    case R.id.rb_2:
                        tag = 1;
                        break;
                    case R.id.rb_3:
                        tag = 2;
                        break;
                    case R.id.rb_4:
                        tag = 3;
                        break;
                    case R.id.rb_5:
                        tag = 4;
                        break;
                    case R.id.rb_6:
                        tag = 5;
                        break;
                    case R.id.rb_7:
                        tag = 6;
                        break;
                }
                listcontry.clear();
                arrayList.clear();
                if (list != null && list.size() > 0) {
                    listcontry.addAll(list.get(tag).getCountryList());
                    for (int j = 0; j < listcontry.size(); j++) {
                        arrayList.add(listcontry.get(j).getRegion_Name());
                    }
                    adapter.notifyDataSetChanged();
                }
            }

        });



        contentLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.putExtra("Country",listcontry.get(i).getRegion_Name());
                intent.putExtra("CountryID",listcontry.get(i).getRegion_ID());
                setResult(111,intent);
                finish();
            }
        });
    }

    public RequestParams getParams() {
        RequestParams params = new RequestParams(PATH_CONTRY);
        return params;
    }
}
